const path = require('path')
const axios = require(path.join(__dirname,"api"))

/**
 * 获取分类下的商品列表
 */
exports.getCateProducts = (cateId, page, size, sort) => {
    const url = `/categories/${cateId}/products?page=${page}&per_page=${size}&sort=${sort}`

    return axios.get(url).then(res=>({list:res.data,total:res.headers["x-total-pages"]}))
    .catch(err=>Promise.reject(err))
}

/**
 * 搜索产品
 */
exports.getSearchProducts = (q,page,size,sort) => {
    const url = `products?page=${page}&per_page=${size}&sort=${sort}&q=${q}`

    //注意：这个接口的响应头中 x-total-pages 的属性  存储的是总条数数据
    return axios.get(url).then(res=>{
        return {
            list:res.data,
            total:res.headers["x-total-pages"]
        }
    }).catch(err=>Promise.reject(err))
}

/**
 * 获取商品详情
 * 
 * isBasic 是否只查询基本信息
 */
exports.getProduct = (goodsId,isBasic = false) => {
    const url = `products/${goodsId}`+ (isBasic ? '': '?include=introduce,category,pictures')

    return axios.get(url).then(res=>res.data).catch(err=>Promise.reject(err))
}